package logic

import (
	"an_ju_ke/models"
	"context"
	"fmt"

	"an_ju_ke/myrpc/internal/svc"
	"an_ju_ke/myrpc/myrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SupplyLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSupplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SupplyLogic {
	return &SupplyLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SupplyLogic) Supply(in *myrpc.SupplyRequest) (*myrpc.SupplyResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	if models.DB.Where("id = ?", in.Id).Limit(1).Find(&user).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在")
	}
	user.Age = int(in.Age)
	user.From = in.From
	user.EMail = in.EMail
	if models.DB.Where("id = ?", user.ID).Updates(&user).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &myrpc.SupplyResponse{}, nil
}
